package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.XfMealInfoService;
import com.ocom.payconfig.vo.DeleteMealVo;
import com.ocom.payconfig.vo.MealNameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MealNameController {

    @Autowired
    private XfMealInfoService mealInfoService;


    //2.1.获取餐段名称信息(getMealNameInfo)
    @GetMapping(PayConfigUrl.URLP_PayConfig_GetMealNameInfo)
    public Result GetMealNameInfo(@RequestParam(required = false) Integer id,
                                  @RequestParam(required = false)Long comId,
                                  @RequestParam(required = false)String mealNo,
                                  @RequestParam(required = false)String mealName){
        if (comId == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        List<EntityXfMealInfo> entityXfMealInfos = mealInfoService.GetMealNameInfo(id, comId, mealNo, mealName);
        Map<String, Object> map = new HashMap<>();
        map.put("total",entityXfMealInfos.size());
        map.put("list",entityXfMealInfos);
        return ResultUtil.success(map);
    }

    // 2.3.添加餐段名称信息(addMealNameInfo)
    @PostMapping(PayConfigUrl.URLP_PayConfig_AddMealNameInfo)
    public Result AddMealNameInfo(@RequestBody @Validated MealNameVo mealNameVo){
        if (mealNameVo.getComId() == null || mealNameVo.getMealNo() == null || mealNameVo.getMealName() == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            mealInfoService.AddMealNameInfo(mealNameVo.getComId(),mealNameVo.getMealNo(),mealNameVo.getMealName());
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultUtil.success();
    }

    // 2.5.修改餐段名称信息(updateMealNameInfo)
    @PostMapping(PayConfigUrl.URLP_PayConfig_UpdateMealNameInfo)
    public Result UpdateMealNameInfo(@RequestBody @Validated MealNameVo mealNameVo){
        if (mealNameVo.getId() == null || mealNameVo.getComId() == null ||
                mealNameVo.getMealNo() == null || mealNameVo.getMealName() == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            mealInfoService.UpdateMealNameInfo(mealNameVo.getId(),mealNameVo.getComId(),
                    mealNameVo.getMealNo(),mealNameVo.getMealName());
        }catch (Exception e){
            e.printStackTrace();
        }

        return ResultUtil.success();
    }

    /**
     * 2.7.删除餐段名称信息(deleteMealNameInfo)
     * */
    @PostMapping(PayConfigUrl.URLP_PayConfig_DeleteMealNameInfo)
    public Result DeleteMealNameInfo(@RequestBody @Validated DeleteMealVo deleteMealVo){
        if (deleteMealVo.getComId() == null ||deleteMealVo.getMealNo() == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            mealInfoService.DeleteMealNameInfo(deleteMealVo.getComId(),deleteMealVo);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ResultUtil.success();
    }

    //获取设备消费汇总报表
    @PostMapping(PayConfigUrl.URLP_PayConfig_getMealConsumeReports)
    public Result getMealConsumeReports(Long comId){
        if (comId == null) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        List<EntityXfMealInfo> list=mealInfoService.getMealConsumeReports(comId);
        return ResultUtil.success(list);
    }
}
